<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      style="margin-left: -12px;margin-top: -16px;">
      <el-form-item label="题目名" prop="subjectId">
        <el-input v-model="queryParams.subjectId" placeholder="请输入题目名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:bind:add']">新增</el-button>
        <span style="margin-left: 20px;">共有 <el-link type="danger" class="font24">15</el-link> 道题，单选题 <el-link
            type="primary" class="font24">5</el-link> 道，多选题 <el-link type="success" class="font24">5</el-link> 道，判断题
          <el-link type="warning" class="font24">5</el-link> 道</span>
      </el-form-item>
    </el-form>


    <el-collapse v-model="activeName" accordion>
      <el-collapse-item title="1、这是测试题目" name="1">
        <div>A、与现实生活一致：与现实生活的流程、逻辑保持一致，遵循用户习惯的语言和概念；</div>
        <div>B、在界面中一致：所有的元素和结构需保持一致，比如：设计样式、图标和文本、元素的位置等。</div>
      </el-collapse-item>
      <el-collapse-item title="2、这是测试题目" name="2">
        <div>A、控制反馈：通过界面样式和交互动效让用户可以清晰的感知自己的操作；</div>
        <div>B、页面反馈：操作后，通过页面元素的变化清晰地展现当前状态。</div>
      </el-collapse-item>
      <el-collapse-item title="3、这是测试题目" name="3">
        <div>A、简化流程：设计简洁直观的操作流程；</div>
        <div>B、清晰明确：语言表达清晰且表意明确，让用户快速理解进而作出决策；</div>
        <div>C、帮助用户识别：界面简单直白，让用户快速识别而非回忆，减少用户记忆负担。</div>
      </el-collapse-item>
      <el-collapse-item title="4、这是测试题目" name="4">
        <div>A、用户决策：根据场景可给予用户操作建议或安全提示，但不能代替用户进行决策；</div>
        <div>B、结果可控：用户可以自由的进行操作，包括撤销、回退和终止当前操作等。</div>
      </el-collapse-item>
    </el-collapse>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bind:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bind:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bind:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bind:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <!-- <el-table v-loading="loading" :data="bindList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="任务备注" align="center" prop="remark" />
      <el-table-column label="" align="center" prop="status" />
      <el-table-column label="${comment}" align="center" prop="subjectId" />
      <el-table-column label="${comment}" align="center" prop="subjectDeptId" />
      <el-table-column label="${comment}" align="center" prop="subjectChapterDeptId" />
      <el-table-column label="${comment}" align="center" prop="subjectTaskId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bind:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bind:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改试题绑定对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form :model="queryBankParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item label="题目标题" prop="title">
          <el-input v-model="queryBankParams.title" placeholder="请输入题目标题" clearable @keyup.enter.native="handleQuery"  class="w200"/>
        </el-form-item>
        <el-form-item label="章节" prop="subjectChapterDeptId">
          <treeselect v-model="queryBankParams.subjectChapterDeptId" :options="subjectChapterDeptIddeptOptions"
            :show-count="true" placeholder="请选择章节" style="width: 140px;" @keyup.enter.native="handleQuery" />
        </el-form-item>


        <el-form-item label="类型" prop="type">
          <el-select v-model="queryBankParams.type" placeholder="题目类型" class="w100" @keyup.enter.native="handleQuery">
            <el-option v-for="item in type" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="难度" prop="level">
          <el-select v-model="queryBankParams.level" placeholder="难易程度" class="w100" @keyup.enter.native="handleQuery">
            <el-option v-for="item in level" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetBankQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="bankList" @selection-change="handleSelectionChange">
        <!-- <el-table-column label="序号" align="center" prop="id" width="80" /> -->
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="章节" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.chapterName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="标题" align="center" prop="title"  width="320"/>
        <el-table-column label="题目类型" align="center" prop="type">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type == 1">单选</el-tag>
            <el-tag type="success" v-if="scope.row.type == 2">多选</el-tag>
            <el-tag type="primary" v-if="scope.row.type == 3">判断</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="难易程度" align="center" prop="level" width="120">
          <template slot-scope="scope">
            <el-rate v-model="scope.row.level" disabled show-score text-color="#ff9900" score-template="" :max="3">
            </el-rate>
          </template>

        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:bank:edit']">选中</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="banktotal > 0" :total="banktotal" :page.sync="queryBankParams.pageNum"
        :limit.sync="queryBankParams.pageSize" @pagination="getBankList" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBind, getBind, delBind, addBind, updateBind } from "@/api/course/task_bind";
import { chapterDeptTreeSelect } from "@/api/course/chapter";
import Treeselect from "@riophae/vue-treeselect";

import { deptTreeSelect } from "@/api/course/dept";
import { listBank } from "@/api/course/bank";

export default {
  name: "Bind",
  components: { Treeselect },
  props: {
    subId: {
      type: Number
    },
    deptId: {
      type: Number
    }
  },
  data() {
    return {
      subjectId: this.subId,
      subjectDeptId: this.deptId,
      subjectChapterDeptIddeptOptions: [],
      activeName: '0',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      banktotal: 0,
      // 试题绑定表格数据
      bindList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        subjectId: this.subId,
        subjectDeptId: null,
        subjectChapterDeptId: null,
        subjectTaskId: null
      },
      queryBankParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        subTitle: null,
        content: null,
        type: null,
        optionA: null,
        optionB: null,
        optionC: null,
        optionD: null,
        optionE: null,
        optionF: null,
        correctA: null,
        correctB: null,
        correctC: null,
        correctD: null,
        correctE: null,
        correctF: null,
        status: null,
        level: null,
        class: null,
        subjectId: this.subId,
        subjectDeptId: null,
        subjectChapterDeptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      bankList: [],
      type: [{
        value: '1',
        label: '单选'
      }, {
        value: '2',
        label: '多选'
      }, {
        value: '3',
        label: '判断'
      }],
      level: [{
        value: '1',
        label: '简单'
      }, {
        value: '2',
        label: '一般'
      }, {
        value: '3',
        label: '困难'
      }],
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询题库列表 */
    getBankList() {
      this.loading = true;
      listBank(this.queryBankParams).then(response => {
        this.bankList = response.rows;
        this.banktotal = response.total;
        if (this.open == false) {
          this.open = true;
          this.title = "添加试题";
        }
        this.loading = false;

      });
    },
    /** 查询章节下拉树结构 */
    getDeptTree() {
      chapterDeptTreeSelect({
        subjectId: this.subId
      }).then(response => {
        this.subjectChapterDeptIddeptOptions = response.data[0].children;
      });
    },
    /** 查询试题绑定列表 */
    getList() {
      this.loading = true;
      listBind(this.queryParams).then(response => {
        this.bindList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        status: null,
        subjectId: null,
        subjectDeptId: null,
        subjectChapterDeptId: null,
        subjectTaskId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() { 
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetQuery() {
      this.resetForm("queryBankForm");
      his.queryParams.pageNum = 1;
      this.handleQuery();
      this.getBankList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getBankList();



    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBind(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改试题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBind(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBind(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除试题绑定编号为"' + ids + '"的数据项？').then(function () {
        return delBind(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bind/export', {
        ...this.queryParams
      }, `bind_${new Date().getTime()}.xlsx`)
    }
  }
};
</script> 

<style lang="scss" scoped>
.font24 {
  font-size: 24px;
  margin-top: -6px;
  padding-left: 8px;
  padding-right: 8px;
}

.w200 {
  width: 160px;
}
.w100 {
  width: 100px;
}
</style>
